layui.config({
    base: '/static/cropper/' //静态资源所在路径
}).extend({
    index: 'lib/index' //主入口模块
}).use(['croppers','upload','layer','form','jquery'], function() {
    var $ = layui.jquery;
    var upload = layui.upload;
    var form = layui.form;
    var layer = layui.layer;
    var upload = layui.upload;
    var croppers = layui.croppers;
    var id=urlQuery("id");

    if(id==null||id==''){
        id = null;
        //添加
    }else {
        $.ajax({
            url: "/user/getInfo",
            data: {id: id},
            type: "get",
            success: function (result) {
                var tUser = result.data;
                    $('#id').val(tUser.id);
                    $('#nickname').val(tUser.nickname);
                    $('#phone').val(tUser.phone);
                    $('#name').val(tUser.name);
                    if (tUser.avatarUrl!='' && tUser.avatarUrl !=null){
                        $("#avatarUrl").val(tUser.avatarUrl);
                        $('#picture').attr('src', tUser.avatarUrl); //图片链接
                    }
                form.render("select");
            },
            error: function () {
                alert("请求失败")
            }
        })
    }

    //头像上传
  /*  upload.render({
        elem: '#test1', //绑定元素
        url: '/tengxun/upload', //上传接口
        // url: '/common/upload', //上传接口
        method: 'POST',
        accept: 'images',
        acceptMime: 'image/!*',
        ext: 'jpg|png|gif|bmp|jpeg',
        before:function (res) {
            layer.load(0, { //icon支持传入0-ueditor
                shade: [0.5, 'gray'], //0.5透明度的灰色背景
            }); //上传loading
        },
        done: function(res){
            if (res.code==200){
                layer.msg('上传成功', {icon: 1,time:2000,shade:0.ueditor});
                $('#picture').attr('src', res.url);//回显
                $("#avatarUrl").val(res.url);
            } else {
                layer.msg('上传失败', {icon: ueditor,time:2000,shade:0.ueditor});
            }
            layer.closeAll('loading'); //关闭loading
        },error: function(){
            var demoText = $('#demoText2');
            demoText.html('<span style="color: #FF4D59;">上传失败</span>');
            layer.closeAll('loading'); //关闭loading
        }
    });*/


    croppers.render({
        elem: '#test1'
        // ,saveW:150     //保存宽度
        // ,saveH:150   //保存高度
        // ,mark:1/1    //选取比例
        ,area:["900px","680px"]  //弹窗宽度
        ,url: "/common/upload"  //图片上传接口返回和（layui 的upload 模块）返回的JOSN一样
        ,done: function(data){ //上传完毕回调
            $('#picture').attr('src', data);
            $('#avatarUrl').val( data);  //成功返回路径存到数据库
        }
    });
    //监听提交
    form.on('submit(demo1)', function(data){
        var data=data.field;
        var regPhone = /^1[3456789]\d{9}$/;
        if(!regPhone.test(data.phone)){
            layer.msg("请输入正确的电话号码", {icon: 2,time:2000,shade:0.2});
            return false;
        }
        $.ajax({
            url:"/user/addAndUpdate",
            type:"post",
            data: data,
            success:function (result) {
                if(result.code==0){
                    layer.msg(result.message, {icon: 1,time:2000,shade:0.2});
                    setInterval(function () {
                    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                    parent.layer.close(index);
                    window.parent.location.reload();
                    },2000)
                }else {
                    layer.msg(result.message, {icon: 2,time:2000,shade:0.2});
                }
            }
        });
        return false;
    });

});